package com.weiqiao.api.service.impl;

import com.google.gson.Gson;
import com.mingsoft.util.StringUtil;
import com.weiqiao.api.domain.CarouseApiEntity;
import com.weiqiao.api.domain.vo.CarouseEntityVO;
import com.weiqiao.api.repository.ICarouseApiRepository;
import com.weiqiao.api.service.ICarouseApiService;
import org.apache.commons.collections.CollectionUtils;
import org.bson.Document;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author ChenYF
 * @Description
 * @Date 2020/7/27 9:57
 */
@Service("CarouseApiServiceImpl")
public class CarouseApiServiceImpl implements ICarouseApiService {

    @Autowired
    private ICarouseApiRepository carouseRepository;

    @Override
    public List<CarouseApiEntity> getCarouseList(CarouseEntityVO vo) {
        List<CarouseApiEntity> shelf = new ArrayList<>();
        Document doc = new Document("company_id", vo.getCompany_id());
        if (!StringUtil.isBlank(vo.getCarouse_id())) {
            doc.append("carouse_id", vo.getCarouse_id());
        }
        List<String> list = carouseRepository.getCarouseList(doc);
        if (CollectionUtils.isNotEmpty(list)) {
            if (list.size() > 1) {
                shelf.add(new Gson().fromJson(list.get(0), CarouseApiEntity.class));
            } else {
                list.forEach(str -> shelf.add(new Gson().fromJson(str, CarouseApiEntity.class)));
            }
        }
        return shelf;
    }
}
